import torch

from operators.mutations.polynomial_mutation import PolynomialMutation


class DE:
    @staticmethod
    def do(problem, dec1, dec2, dec3, CR=1, F=0.5, proM=1, disM=20):
        """
        差分进化
        :param problem:
        :param dec1:
        :param dec2:
        :param dec3:
        :param CR:
        :param F:
        :param proM:
        :param disM:
        :return:
        """
        [pop_size, var_dim] = dec1.shape
        site = torch.rand(pop_size, var_dim) < CR
        offspring_dec = dec1.clone()
        offspring_dec[site] = offspring_dec[site] + F * (dec2[site] - dec3[site])
        offspring_dec = problem.repair_decision(offspring_dec)
        return PolynomialMutation.do(offspring_dec, problem, disM, proM)
